package com.sahala.downimage.http;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.sahala.downimage.domain.ImageDO;

public class HttpUtils {

	private static Logger logger = Logger.getLogger(HttpUtils.class);

	public static Set<Integer> getListPageId(int pageNum) {
		HttpClient client = new HttpClient();
		String url = "http://www.duitang.com/blogs/tag/hot/"
				+ pageNum
				+ "/?tags=%E6%89%8B%E5%B7%A5%2C%E7%BB%A3%E6%B3%95%2C%E6%89%8B%E4%BD%9C%2C%E6%8A%98%E7%BA%B8%2C%E6%8A%98%E7%BA%B8&_type=";
		GetMethod get = new GetMethod(url);

		Set<Integer> result = new HashSet<Integer>();
		try {
			int statusCode = client.executeMethod(get);
			if (statusCode != 200) {
				logger.error("getListPageId@statusCode is not 200,page:" + url);
				return null;
			}
			String responseContext = get.getResponseBodyAsString();
			if (StringUtils.isBlank(responseContext)) {
				return result;
			}
			JSONObject data = JSONObject.fromObject(responseContext);
			JSONObject blogs = (JSONObject) data.get("data");
			JSONArray array = (JSONArray) blogs.get("blogs");
			for (int i = 0; i < array.size(); i++) {
				JSONObject value = (JSONObject) array.get(i);
				result.add((Integer) value.get("id"));
			}

		} catch (HttpException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally {
			client = null;
		}
		return result;
	}

	public static List<ImageDO> getImageDoList(long pageId) {
		List<ImageDO> result = new ArrayList<ImageDO>();
		HttpClient client = new HttpClient();
		String pageUrl = "http://www.duitang.com/people/mblog/" + pageId
				+ "/detail/";
		GetMethod get = new GetMethod(pageUrl);

		try {
			int statusCode = client.executeMethod(get);
			if (statusCode != 200) {
				logger.error("getImageLink@statusCode is not 200,page:"
						+ pageUrl);
				return result;
			}

			String responseContent = get.getResponseBodyAsString();

			Pattern p = Pattern.compile("<img[^>]+>");

			Matcher m = p.matcher(responseContent);
			while (m.find()) {
				String img = m.group();
				if (img.contains("" + pageId) && img.contains("data-rootid")) {
					Pattern pattern = Pattern
							.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
					Matcher matcher = pattern.matcher(img);
					while (matcher.find()) {
						String imageUrl = matcher.group(1);
						String[] values = StringUtils.split(imageUrl, "/");
						ImageDO image = new ImageDO();
						image.setImageUrl(imageUrl);
						for (String value : values) {
							if (value.matches("[0-9]{2}")) {
								image.setGenDate(value);
							}
							if (value.matches("[0-9]{6}")) {
								image.setYearMonth(value);
							}
						}
						image.setImgName(values[values.length - 1]);
						result.add(image);
					}
				}
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			client = null;
		}
		return result;
	}
	
	public static void savePicToDisk(String rootPath,ImageDO image){
		HttpClient client = new HttpClient();
		GetMethod get = new GetMethod(image.getImageUrl());
		OutputStream out = null;
		try {
			int statusCode = client.executeMethod(get);
			if (statusCode != 200) {
				logger.error("getImageLink@statusCode is not 200,page:"
						+ image.getImageUrl());
				return;
			}
			byte bodys[] = get.getResponseBody();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String filePath = rootPath+File.separator+sdf.format(new Date())+File.separator;
			File dir = new File(filePath);
			if(!dir.exists()){
				dir.mkdirs();
			}
			out = new FileOutputStream(new File(filePath+image.getImgName()));
			out.write(bodys);
			out.flush();
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try{
				out.close();
			}catch(Exception e){
				e.printStackTrace();
			}
			client = null;
		}
		
	}

	public static void main(String[] args) {
		List<ImageDO> imageUrls = getImageDoList(33556500);
		
		savePicToDisk("E:\\pic", imageUrls.get(0));
		
		System.out.println(imageUrls);

	}
}
